﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using Model.AuthModel;
using IDAL.AuthModel;
namespace SQLServerDAL.AuthModel
{
    /// <summary>
    /// 通用组件，权限模型，数据层，成员角色
    /// </summary>
    public class StaffRolesDAL : DalBase, IStaffRoles
    {
        public StaffRolesDAL() 
        {
            Schema = "am.";
            ViewTableName = "";
            ViewTableList = new string[] { 
                "V_StaffRoles", "V_StaffRolesAuth"
            };
            FieldsList = new string[] { 
                "StaffRolesID, StaffID, Username, Email, IsLock, LastLogin, LastAccess, RolesID, RolesName"
                ,"StaffRolesID, StaffID, Username, Email, IsLock, LastLogin, LastAccess, RolesID, RolesName, RolesAuthID, AuthID, ModuleID, ModuleName, ModuleLink, ModuleMenu, ModuleTypeID, ModuleTypeName, FuncID, FuncName, FuncMenu"
                ,"ModuleTypeID, ModuleTypeName"
            };
        }
        #region IStaffRole 成员

        /// <summary>
        /// 更新成员角色
        /// </summary>
        /// <param name="staffID"></param>
        /// <param name="roles"></param>
        /// <returns></returns>
        public bool UpdateStaffRoles(int staffID, string roles)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@StaffID",SqlDbType.Int)
                ,new SqlParameter("@RolesList",SqlDbType.VarChar,100)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = staffID;
            parm[i++].Value = roles;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure
                , base.Schema + "UpdateStaffRoles", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 成员角色列表
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IList<StaffRolesInfo> ListStaffRoles(StaffRolesCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", FieldsList[0], Schema + ViewTableList[0]);
            if (condition.StaffID > 0)
                SqlBuilder.AppendFormat(" AND StaffID = {0} ", condition.StaffID);
            if (condition.RolesID > 0)
                SqlBuilder.AppendFormat(" AND RolesID = {0} ", condition.RolesID);
            if (!string.IsNullOrEmpty(condition.KeyWord))
                SqlBuilder.AppendFormat(" AND (Username LIKE '%{0}%' OR Email LIKE '%{0}%') ");
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            List<StaffRolesInfo> list = new List<StaffRolesInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {//StaffRolesID, StaffID, Username, Email, IsLock, LastLogin, LastAccess, RolesID, RolesName
                    int i = 0;
                    StaffRolesInfo info = new StaffRolesInfo();
                    info.StaffRoleID = dr.GetInt32(i++);

                    info.Staff = new StaffInfo();
                    info.Staff.StaffID = dr.GetInt32(i++);
                    info.Staff.Username = dr.GetString(i++);
                    info.Staff.Email = dr.GetString(i++);
                    info.Staff.IsLock = dr.GetString(i++);
                    info.Staff.LastLogin = dr.GetDateTime(i++);
                    info.Staff.LastAccess = dr.GetDateTime(i++);

                    info.Roles = new RolesInfo();
                    info.Roles.RolesID = dr.GetInt32(i++);
                    info.Roles.RolesName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        /// <summary>
        /// 成员角色列表
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IList<StaffRolesInfo> ListStaffRoles(StaffRolesCondition condition, out int recordCount)
        {
            Wheres = new System.Text.StringBuilder(" 1 = 1 ");
            if (condition.StaffID > 0)
                Wheres.AppendFormat(" AND StaffID = {0} ", condition.StaffID);
            if (condition.RolesID > 0)
                Wheres.AppendFormat(" AND RolesID = {0} ", condition.RolesID);
            if (!string.IsNullOrEmpty(condition.KeyWord))
                Wheres.AppendFormat(" AND (Username LIKE '%{0}%' OR Email LIKE '%{0}%') ");
            recordCount = base.PageListCounter(Schema + ViewTableList[0], Wheres.ToString());
            SqlDataReader dr = base.PageListRecord(FieldsList[0], Schema + ViewTableList[0], Wheres.ToString()
                , condition.OrderBy, condition.PageIndex, condition.PageSize);
            List<StaffRolesInfo> list = new List<StaffRolesInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    int i = 0;
                    StaffRolesInfo info = new StaffRolesInfo();
                    info.StaffRoleID = dr.GetInt32(i++);

                    info.Staff = new StaffInfo();
                    info.Staff.StaffID = dr.GetInt32(i++);
                    info.Staff.Username = dr.GetString(i++);
                    info.Staff.Email = dr.GetString(i++);
                    info.Staff.IsLock = dr.GetString(i++);
                    info.Staff.LastLogin = dr.GetDateTime(i++);
                    info.Staff.LastAccess = dr.GetDateTime(i++);

                    info.Roles = new RolesInfo();
                    info.Roles.RolesID = dr.GetInt32(i++);
                    info.Roles.RolesName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        /// <summary>
        /// 检查成员权限
        /// </summary>
        /// <param name="staffID"></param>
        /// <param name="moduleLink"></param>
        /// <param name="funcName"></param>
        /// <returns></returns>
        public bool CheckRolesAuthByStaff(int staffID, string moduleLink, string funcName)
        {
            SqlBuilder = new System.Text.StringBuilder("SELECT COUNT(1) ");
            SqlBuilder.AppendFormat(" FROM {0} ", Schema + ViewTableList[1]);
            SqlBuilder.AppendFormat(" WHERE StaffID = {0} AND ModuleLink = '{1}' AND FuncName = '{2}' ", staffID, moduleLink, funcName);
            object obj = SqlHelper.ExecuteScalar(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            return obj != null ? int.Parse(obj.ToString()) > 0 : false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<StaffRolesInfo> ListRolesAuthModuleByStaff(StaffRolesCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", FieldsList[1], Schema + ViewTableList[1]);
            if (condition.ModuleTypeID > 0)
                SqlBuilder.AppendFormat(" AND ModuleTypeID = {0} ", condition.ModuleTypeID);
            if (condition.RolesID > 0)
                SqlBuilder.AppendFormat(" AND RolesID = {0} ", condition.RolesID);
            if (condition.StaffID > 0)
                SqlBuilder.AppendFormat(" AND StaffID = {0} ", condition.StaffID);
            if (!string.IsNullOrEmpty(condition.ModuleMenu))
                SqlBuilder.AppendFormat(" AND ModuleMenu = '{0}'", condition.ModuleMenu);
            if (!string.IsNullOrEmpty(condition.FuncMenu))
                SqlBuilder.AppendFormat(" AND FuncMenu = '{0}'", condition.FuncMenu);
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0} ", condition.OrderBy);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            List<StaffRolesInfo> list = new List<StaffRolesInfo>();
            if (dr != null)
            {
                //StaffRolesID, StaffID, Username, Email, IsLock, LastLogin, LastAccess, RolesID, RolesName, RolesAuthID
                //, AuthID, ModuleID, ModuleName, ModuleLink, ModuleMenu, ModuleTypeID, ModuleTypeName, FuncID, FuncName, FuncMenu
                while (dr.Read())
                {
                    int i = 0;
                    StaffRolesInfo info = new StaffRolesInfo();
                    info.StaffRoleID = dr.GetInt32(i++);

                    info.Staff = new StaffInfo();
                    info.Staff.StaffID = dr.GetInt32(i++);
                    info.Staff.Username = dr.GetString(i++);
                    info.Staff.Email = dr.GetString(i++);
                    info.Staff.IsLock = dr.GetString(i++);
                    info.Staff.LastLogin = dr.GetDateTime(i++);
                    info.Staff.LastAccess = dr.GetDateTime(i++);

                    info.Roles = new RolesInfo();
                    info.Roles.RolesID = dr.GetInt32(i++);
                    info.Roles.RolesName = dr.GetString(i++);

                    info.Roles.RolesAuth = new RolesAuthInfo();
                    info.Roles.RolesAuth.RolesAuthID = dr.GetInt32(i++);

                    info.Roles.RolesAuth.Auth = new AuthInfo();
                    info.Roles.RolesAuth.Auth.AuthID = dr.GetInt32(i++);

                    info.Roles.RolesAuth.Auth.Module = new ModuleInfo();
                    info.Roles.RolesAuth.Auth.Module.ModuleID = dr.GetInt32(i++);
                    info.Roles.RolesAuth.Auth.Module.ModuleName = dr.GetString(i++);
                    info.Roles.RolesAuth.Auth.Module.ModuleLink = dr.GetString(i++);
                    info.Roles.RolesAuth.Auth.Module.ModuleMenu = dr.GetString(i++);

                    info.Roles.RolesAuth.Auth.Module.ModuleType = new ModuleTypeInfo();
                    info.Roles.RolesAuth.Auth.Module.ModuleType.ModuleTypeID = dr.GetInt32(i++);
                    info.Roles.RolesAuth.Auth.Module.ModuleType.ModuleTypeName = dr.GetString(i++);

                    info.Roles.RolesAuth.Auth.Func = new FuncInfo();
                    info.Roles.RolesAuth.Auth.Func.FuncID = dr.GetInt32(i++);
                    info.Roles.RolesAuth.Auth.Func.FuncName = dr.GetString(i++);
                    info.Roles.RolesAuth.Auth.Func.FuncMenu = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        public IList<StaffRolesInfo> ListRolesAuthModuleTypeByStaff(StaffRolesCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1", FieldsList[2], Schema + ViewTableList[1]);
            if (condition.StaffID > 0)
                SqlBuilder.AppendFormat(" AND StaffID = {0}", condition.StaffID);
            if (!string.IsNullOrEmpty(condition.ModuleMenu))
                SqlBuilder.AppendFormat(" AND ModuleMenu = '{0}'", condition.ModuleMenu);
            if (!string.IsNullOrEmpty(condition.FuncMenu))
                SqlBuilder.AppendFormat(" AND FuncMenu = '{0}'", condition.FuncMenu);
            SqlBuilder.AppendFormat(" GROUP BY ModuleTypeID, ModuleTypeName ");
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0}", condition.OrderBy);
            List<StaffRolesInfo> list = new List<StaffRolesInfo>();
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            if (dr != null)
            {
                while (dr.Read())
                {
                    int i = 0;
                    StaffRolesInfo info = new StaffRolesInfo();
                    info.Roles=new RolesInfo();
                    info.Roles.RolesAuth = new RolesAuthInfo();
                    info.Roles.RolesAuth.Auth = new AuthInfo();
                    info.Roles.RolesAuth.Auth.Module = new ModuleInfo();
                    info.Roles.RolesAuth.Auth.Module.ModuleType = new ModuleTypeInfo();
                    info.Roles.RolesAuth.Auth.Module.ModuleType.ModuleTypeID = dr.GetInt32(i++);
                    info.Roles.RolesAuth.Auth.Module.ModuleType.ModuleTypeName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        #endregion
    }
}
